{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# furniture_moving"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/furniture_moving.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/furniture_moving.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Moving furnitures (scheduling) problem in Google CP Solver.\n",
    "\n",
    "  Marriott & Stukey: 'Programming with constraints', page  112f\n",
    "\n",
    "  The model implements an experimental decomposition of the\n",
    "  global constraint cumulative.\n",
    "\n",
    "  Compare with the following models:\n",
    "  * ECLiPSE: http://www.hakank.org/eclipse/furniture_moving.ecl\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/furniture_moving.mzn\n",
    "  * Comet: http://www.hakank.org/comet/furniture_moving.co\n",
    "  * Choco: http://www.hakank.org/choco/FurnitureMoving.java\n",
    "  * Gecode: http://www.hakank.org/gecode/furniture_moving.cpp\n",
    "  * JaCoP: http://www.hakank.org/JaCoP/FurnitureMoving.java\n",
    "  * SICStus: http://hakank.org/sicstus/furniture_moving.pl\n",
    "  * Zinc: http://hakank.org/minizinc/furniture_moving.zinc\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "#\n",
    "# Decompositon of cumulative.\n",
    "#\n",
    "# Inspired by the MiniZinc implementation:\n",
    "# http://www.g12.csse.unimelb.edu.au/wiki/doku.php?id=g12:zinc:lib:minizinc:std:cumulative.mzn&s[]=cumulative\n",
    "# The MiniZinc decomposition is discussed in the paper:\n",
    "# A. Schutt, T. Feydy, P.J. Stuckey, and M. G. Wallace.\n",
    "# 'Why cumulative decomposition is not as bad as it sounds.'\n",
    "# Download:\n",
    "# http://www.cs.mu.oz.au/%7Epjs/rcpsp/papers/cp09-cu.pdf\n",
    "# http://www.cs.mu.oz.au/%7Epjs/rcpsp/cumu_lazyfd.pdf\n",
    "#\n",
    "#\n",
    "# Parameters:\n",
    "#\n",
    "# s: start_times    assumption: array of IntVar\n",
    "# d: durations      assumption: array of int\n",
    "# r: resources      assumption: array of int\n",
    "# b: resource limit assumption: IntVar or int\n",
    "#\n",
    "def my_cumulative(solver, s, d, r, b):\n",
    "\n",
    "  # tasks = [i for i in range(len(s))]\n",
    "  tasks = [i for i in range(len(s)) if r[i] > 0 and d[i] > 0]\n",
    "  times_min = min([s[i].Min() for i in tasks])\n",
    "  times_max = max([s[i].Max() + max(d) for i in tasks])\n",
    "  for t in range(times_min, times_max + 1):\n",
    "    bb = []\n",
    "    for i in tasks:\n",
    "      c1 = solver.IsLessOrEqualCstVar(s[i], t)  # s[i] <= t\n",
    "      c2 = solver.IsGreaterCstVar(s[i] + d[i], t)  # t < s[i] + d[i]\n",
    "      bb.append(c1 * c2 * r[i])\n",
    "    solver.Add(solver.Sum(bb) <= b)\n",
    "\n",
    "  # Somewhat experimental:\n",
    "  # This constraint is needed to contrain the upper limit of b.\n",
    "  if not isinstance(b, int):\n",
    "    solver.Add(b <= sum(r))\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Furniture moving\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 4\n",
    "  duration = [30, 10, 15, 15]\n",
    "  demand = [3, 1, 3, 2]\n",
    "  upper_limit = 160\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  start_times = [\n",
    "      solver.IntVar(0, upper_limit, \"start_times[%i]\" % i) for i in range(n)\n",
    "  ]\n",
    "  end_times = [\n",
    "      solver.IntVar(0, upper_limit * 2, \"end_times[%i]\" % i) for i in range(n)\n",
    "  ]\n",
    "  end_time = solver.IntVar(0, upper_limit * 2, \"end_time\")\n",
    "\n",
    "  # number of needed resources, to be minimized\n",
    "  num_resources = solver.IntVar(0, 10, \"num_resources\")\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  for i in range(n):\n",
    "    solver.Add(end_times[i] == start_times[i] + duration[i])\n",
    "\n",
    "  solver.Add(end_time == solver.Max(end_times))\n",
    "\n",
    "  my_cumulative(solver, start_times, duration, demand, num_resources)\n",
    "\n",
    "  #\n",
    "  # Some extra constraints to play with\n",
    "  #\n",
    "\n",
    "  # all tasks must end within an hour\n",
    "  # solver.Add(end_time <= 60)\n",
    "\n",
    "  # All tasks should start at time 0\n",
    "  # for i in range(n):\n",
    "  #    solver.Add(start_times[i] == 0)\n",
    "\n",
    "  # limitation of the number of people\n",
    "  # solver.Add(num_resources <= 3)\n",
    "\n",
    "  #\n",
    "  # objective\n",
    "  #\n",
    "  # objective = solver.Minimize(end_time, 1)\n",
    "  objective = solver.Minimize(num_resources, 1)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  solution = solver.Assignment()\n",
    "  solution.Add(start_times)\n",
    "  solution.Add(end_times)\n",
    "  solution.Add(end_time)\n",
    "  solution.Add(num_resources)\n",
    "\n",
    "  db = solver.Phase(start_times, solver.CHOOSE_FIRST_UNBOUND,\n",
    "                    solver.ASSIGN_MIN_VALUE)\n",
    "\n",
    "  #\n",
    "  # result\n",
    "  #\n",
    "  solver.NewSearch(db, [objective])\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    num_solutions += 1\n",
    "    print(\"num_resources:\", num_resources.Value())\n",
    "    print(\"start_times  :\", [start_times[i].Value() for i in range(n)])\n",
    "    print(\"duration     :\", [duration[i] for i in range(n)])\n",
    "    print(\"end_times    :\", [end_times[i].Value() for i in range(n)])\n",
    "    print(\"end_time     :\", end_time.Value())\n",
    "    print()\n",
    "\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
